package cli;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class Settings {
	// location of settings file
	private static final String settingsFile = "./settings/Settings.xml";
	
	public static final String DATE_FORMAT = "yyyy/MM/dd HH:mm:ss";
	
	/**
	 * read logfile location and name from settings.xml file
	 * @return the name of the log file
	 */
	public static String getLogFile() {
		String loc, name;
		
		try {
			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			File f = new File ( settingsFile );
			
			Document doc = docBuilder.parse ( f );

			// normalize text representation
			doc.getDocumentElement ().normalize ();

			NodeList listOfLogs = doc.getElementsByTagName("logging");
			int totalLogs = listOfLogs.getLength();

			if (totalLogs > 0 ) {
				Node firstLogNode = listOfLogs.item(0);
				if(firstLogNode.getNodeType() == Node.ELEMENT_NODE)  {

					Element firstLogElement = (Element)firstLogNode;

					//-------
					NodeList firstLocationList = firstLogElement.getElementsByTagName("location");
					Element firstLocationElement = (Element)firstLocationList.item(0);
					NodeList textLocationList = firstLocationElement.getChildNodes();
					loc = ((Node)textLocationList.item(0)).getNodeValue().trim();
						
					NodeList nameList = firstLogElement.getElementsByTagName("name");
					Element nameElement = (Element)nameList.item(0);

					NodeList textNameList = nameElement.getChildNodes();
					name =  ((Node)textNameList.item(0)).getNodeValue().trim();
					
					// succes
					return loc + "/" + name;

				} else {
					return null;
				} 

			} else {
				return null;
			}


		}catch (SAXParseException err) {
			System.out.println ("** Parsing error" + ", line " 
					+ err.getLineNumber () + ", uri " + err.getSystemId ());
			System.out.println(" " + err.getMessage ());

		}catch (SAXException e) {
			Exception x = e.getException ();
			((x == null) ? e : x).printStackTrace ();

		}catch (Throwable t) {
			t.printStackTrace ();
		}
		return null;
	}


	/**
	 * read general temp directory from settings.xml file
	 * @return the directory where to store general temp files 
	 */
	public static String getTempDir() {
		String dir;
		
		try {
			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			File f = new File ( settingsFile );
			
			Document doc = docBuilder.parse ( f );

			// normalize text representation
			doc.getDocumentElement ().normalize ();

			NodeList listOfLogs = doc.getElementsByTagName("tempDirs");
			int totalLogs = listOfLogs.getLength();

			if (totalLogs > 0 ) {
				Node firstLogNode = listOfLogs.item(0);
				if(firstLogNode.getNodeType() == Node.ELEMENT_NODE)  {

					Element firstLogElement = (Element)firstLogNode;

					//-------
					NodeList firstLocationList = firstLogElement.getElementsByTagName("tempDir");
					Element firstLocationElement = (Element)firstLocationList.item(0);
					NodeList textLocationList = firstLocationElement.getChildNodes();
					dir = ((Node)textLocationList.item(0)).getNodeValue().trim();
						
					return dir;
					
				} else {
					return null;
				} 

			} else {
				return null;
			}


		}catch (SAXParseException err) {
			System.out.println ("** Parsing error" + ", line " 
					+ err.getLineNumber () + ", uri " + err.getSystemId ());
			System.out.println(" " + err.getMessage ());

		}catch (SAXException e) {
			Exception x = e.getException ();
			((x == null) ? e : x).printStackTrace ();

		}catch (Throwable t) {
			t.printStackTrace ();
		}
		return null;
	}
	
	
	/**
	 * read image temporarely directory (tempImageDirfrom settings.xml file
	 * @return the directory where to store temp image files
	 */
	public static String getTempImageDir() {
		String dir;
		
		try {
			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			File f = new File ( settingsFile );
			
			Document doc = docBuilder.parse ( f );

			// normalize text representation
			doc.getDocumentElement ().normalize ();

			NodeList listOfLogs = doc.getElementsByTagName("tempDirs");
			int totalLogs = listOfLogs.getLength();

			if (totalLogs > 0 ) {
				Node firstLogNode = listOfLogs.item(0);
				if(firstLogNode.getNodeType() == Node.ELEMENT_NODE)  {

					Element firstLogElement = (Element)firstLogNode;

					//-------
					NodeList firstLocationList = firstLogElement.getElementsByTagName("tempImageDir");
					Element firstLocationElement = (Element)firstLocationList.item(0);
					NodeList textLocationList = firstLocationElement.getChildNodes();
					dir = ((Node)textLocationList.item(0)).getNodeValue().trim();
						
					return dir;
					
				} else {
					return null;
				} 

			} else {
				return null;
			}


		}catch (SAXParseException err) {
			System.out.println ("** Parsing error" + ", line " 
					+ err.getLineNumber () + ", uri " + err.getSystemId ());
			System.out.println(" " + err.getMessage ());

		}catch (SAXException e) {
			Exception x = e.getException ();
			((x == null) ? e : x).printStackTrace ();

		}catch (Throwable t) {
			t.printStackTrace ();
		}
		return null;
	}
	
	/**
	 * read index temporarely directory (tempIndexDir from settings.xml file
	 * @return the directory where to store temp index files
	 */
	public static String getTempIndexDir() {
		String dir;
		
		try {
			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			File f = new File ( settingsFile );
			
			Document doc = docBuilder.parse ( f );

			// normalize text representation
			doc.getDocumentElement ().normalize ();

			NodeList listOfLogs = doc.getElementsByTagName("tempDirs");
			int totalLogs = listOfLogs.getLength();

			if (totalLogs > 0 ) {
				Node firstLogNode = listOfLogs.item(0);
				if(firstLogNode.getNodeType() == Node.ELEMENT_NODE)  {

					Element firstLogElement = (Element)firstLogNode;

					//-------
					NodeList firstLocationList = firstLogElement.getElementsByTagName("tempIndexDir");
					Element firstLocationElement = (Element)firstLocationList.item(0);
					NodeList textLocationList = firstLocationElement.getChildNodes();
					dir = ((Node)textLocationList.item(0)).getNodeValue().trim();
						
					return dir;
					
				} else {
					return null;
				} 

			} else {
				return null;
			}


		}catch (SAXParseException err) {
			System.out.println ("** Parsing error" + ", line " 
					+ err.getLineNumber () + ", uri " + err.getSystemId ());
			System.out.println(" " + err.getMessage ());

		}catch (SAXException e) {
			Exception x = e.getException ();
			((x == null) ? e : x).printStackTrace ();

		}catch (Throwable t) {
			t.printStackTrace ();
		}
		return null;
	}


	/**
	 * read the search image dir from settings.xml file<br/>
	 * This directory will be used for the matlab algo. Images in this dir
	 * will be included in the search
	 * @return the omero user name
	 */
	public static String getSearchImageDir() {
		String pwd;
		
		try {
			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			File f = new File ( settingsFile );
			
			Document doc = docBuilder.parse ( f );

			// normalize text representation
			doc.getDocumentElement ().normalize ();

			NodeList listOfLogs = doc.getElementsByTagName("searchDirs");
			int totalLogs = listOfLogs.getLength();

			if (totalLogs > 0 ) {
				Node firstLogNode = listOfLogs.item(0);
				if(firstLogNode.getNodeType() == Node.ELEMENT_NODE)  {

					Element firstLogElement = (Element)firstLogNode;

					//-------
					NodeList firstLocationList = firstLogElement.getElementsByTagName("imageDir");
					Element firstLocationElement = (Element)firstLocationList.item(0);
					NodeList textLocationList = firstLocationElement.getChildNodes();
					pwd = ((Node)textLocationList.item(0)).getNodeValue().trim();
						
					return pwd;
					
				} else {
					return null;
				} 

			} else {
				return null;
			}


		}catch (SAXParseException err) {
			System.out.println ("** Parsing error" + ", line " 
					+ err.getLineNumber () + ", uri " + err.getSystemId ());
			System.out.println(" " + err.getMessage ());

		}catch (SAXException e) {
			Exception x = e.getException ();
			((x == null) ? e : x).printStackTrace ();

		}catch (Throwable t) {
			t.printStackTrace ();
		}
		return null;
	}

	/**
	 * read the search image INDEX dir from settings.xml file<br/>
	 * This directory will be used for the matlab algo. Index files in this dir
	 * will be included in the search
	 * @return the omero user name
	 */
	public static String getSearchIndexDir() {
		String pwd;
		
		try {
			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			File f = new File ( settingsFile );
			
			Document doc = docBuilder.parse ( f );

			// normalize text representation
			doc.getDocumentElement ().normalize ();

			NodeList listOfLogs = doc.getElementsByTagName("searchDirs");
			int totalLogs = listOfLogs.getLength();

			if (totalLogs > 0 ) {
				Node firstLogNode = listOfLogs.item(0);
				if(firstLogNode.getNodeType() == Node.ELEMENT_NODE)  {

					Element firstLogElement = (Element)firstLogNode;

					//-------
					NodeList firstLocationList = firstLogElement.getElementsByTagName("indexDir");
					Element firstLocationElement = (Element)firstLocationList.item(0);
					NodeList textLocationList = firstLocationElement.getChildNodes();
					pwd = ((Node)textLocationList.item(0)).getNodeValue().trim();
						
					return pwd;
					
				} else {
					return null;
				} 

			} else {
				return null;
			}
		}catch (SAXParseException err) {
			System.out.println ("** Parsing error" + ", line " 
					+ err.getLineNumber () + ", uri " + err.getSystemId ());
			System.out.println(" " + err.getMessage ());

		}catch (SAXException e) {
			Exception x = e.getException ();
			((x == null) ? e : x).printStackTrace ();

		}catch (Throwable t) {
			t.printStackTrace ();
		}
		return null;
	}
		
	/**
	 * read the search result directory from settings.xml file<br/>
	 * This directory will be used for the matlab algo. Results of the 
	 * search will be placed in this directory  
	 * @return result directory
	 */
	public static String getSearchResultDir() {
		String pwd;
		
		try {
			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			File f = new File ( settingsFile );
			
			Document doc = docBuilder.parse ( f );

			// normalize text representation
			doc.getDocumentElement ().normalize ();

			NodeList listOfLogs = doc.getElementsByTagName("searchDirs");
			int totalLogs = listOfLogs.getLength();

			if (totalLogs > 0 ) {
				Node firstLogNode = listOfLogs.item(0);
				if(firstLogNode.getNodeType() == Node.ELEMENT_NODE)  {

					Element firstLogElement = (Element)firstLogNode;

					//-------
					NodeList firstLocationList = firstLogElement.getElementsByTagName("resultDir");
					Element firstLocationElement = (Element)firstLocationList.item(0);
					NodeList textLocationList = firstLocationElement.getChildNodes();
					pwd = ((Node)textLocationList.item(0)).getNodeValue().trim();
						
					return pwd;
					
				} else {
					return null;
				} 

			} else {
				return null;
			}
		}catch (SAXParseException err) {
			System.out.println ("** Parsing error" + ", line " 
					+ err.getLineNumber () + ", uri " + err.getSystemId ());
			System.out.println(" " + err.getMessage ());

		}catch (SAXException e) {
			Exception x = e.getException ();
			((x == null) ? e : x).printStackTrace ();

		}catch (Throwable t) {
			t.printStackTrace ();
		}
		return null;
	}
		
}
